package utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
    public class DisplayUtil
    {
        public static const TOP:String = "top";
        public static const BOTTOM:String = "bottom";
        public static const LEFT:String = "left";
        public static const RIGHT:String = "right";
        public static const TOPLEFT:String = "topleft";
        public static const TOPRIGHT:String = "topright";
        public static const BOTTOMLEFT:String = "bottomleft";
        public static const BOTTOMRIGHT:String = "bottomright";
        
        private static var _sparkArr:Array;
        private static var timer:EnterFrameTimer;
        private static var _isHeilight:Boolean = false;

        public function DisplayUtil()
        {
        }

        public static function localToGlobal(param1:XY, param2:DisplayObjectContainer) : XY
        {
            var _loc_3:Point = null;
            if (param2 && param2.stage != null)
            {
                _loc_3 = new Point(param1.x, param1.y);
                _loc_3 = param2.localToGlobal(_loc_3);
                return new XY(_loc_3.x, _loc_3.y);
            }
            return null;
        }

        public static function globalToLocal(param1:XY, param2:DisplayObjectContainer) : XY
        {
            var _loc_3:Point = null;
            if (param2 && param2.stage != null)
            {
                _loc_3 = new Point(param1.x, param1.y);
                _loc_3 = param2.globalToLocal(_loc_3);
                return new XY(_loc_3.x, _loc_3.y);
            }
            return null;
        }

        public static function addChildAt(param1:DisplayObject, param2:DisplayObjectContainer, param3:uint) : void
        {
            if (param3 >= 0)
            {
                if (param2.numChildren >= param3 && param1 != null && param2 != null)
                {
                    param2.addChildAt(param1, param3);
                }
            }
        }

        public static function lineup(param1:Array, param2:int, param3:int = 0, param4:int = 0) : void
        {
            var _loc_6:* = undefined;
            var _loc_5:int = 0;
            while (_loc_5 < param1.length)
            {
                
                _loc_6 = param1[_loc_5];
                _loc_6.x = param3;
                _loc_6.y = param4;
                param3 = _loc_6.x + _loc_6.width + param2;
                _loc_5++;
            }
        }

        public static function moveToTop(param1:DisplayObject) : void
        {
            var _loc_2:DisplayObjectContainer = null;
            if (param1.parent != null)
            {
                _loc_2 = param1.parent;
                _loc_2.setChildIndex(param1, (_loc_2.numChildren - 1));
            }
        }

        public static function isInDisplayTree(param1:DisplayObject, param2:DisplayObjectContainer) : Boolean
        {
            var _loc_3:* = param1.parent;
            while (_loc_3 != null)
            {
                
                if (_loc_3 == param2)
                {
                    return true;
                }
                _loc_3 = _loc_3.parent;
            }
            return false;
        }

        public static function getDisplayObjectStageXY(param1:DisplayObject) : XY
        {
            var _loc_2:XY = null;
            var _loc_3:Point = null;
            if (param1 && param1.stage != null)
            {
                _loc_2 = new XY();
                _loc_3 = param1.localToGlobal(new Point());
                _loc_2.x = _loc_3.x;
                _loc_2.y = _loc_3.y;
                return _loc_2;
            }
            return null;
        }

        private static function showHighLight(param1:DisplayObject, param2:int, param3:Array) : void
        {
            ColorUtil.highLight(param1);
        }

        public static function toRightOf(param1:DisplayObject, param2:DisplayObject, param3:int = 0) : void
        {
            toXof(param1, param2, "right", param3);
        }

        public static function toLeftOf(param1:DisplayObject, param2:DisplayObject, param3:int = 0) : void
        {
            toXof(param1, param2, LEFT, param3);
        }

        public static function switchVisible(param1:DisplayObject) : void
        {
            param1.visible = !param1.visible;
        }

        public static function removeAllChild(param1:DisplayObjectContainer) : void
        {
            if (param1 != null)
            {
                while (param1.numChildren > 0)
                {
                    
                    param1.removeChildAt(0);
                }
            }
        }

        public static function addChildAfter(param1:DisplayObject, param2:DisplayObject, param3:DisplayObjectContainer) : void
        {
            var _loc_4:int = 0;
            while (_loc_4 < param3.numChildren)
            {
                
                if (param3.getChildAt(_loc_4) == param2)
                {
                    param3.addChildAt(param1, (_loc_4 + 1));
                    break;
                }
                _loc_4++;
            }
        }

        public static function outterAlign(param1:DisplayObject, param2:DisplayObject, param3:String, param4:int = 0, param5:int = 0) : void
        {
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            switch(param3)
            {
                case "top":
                {
                    _loc_6 = param2.x + param4;
                    _loc_7 = param1.y - param2.height + param5;
                    break;
                }
                case "right":
                {
                    _loc_6 = param1.x + param1.width + param4;
                    _loc_7 = param2.y + param5;
                    break;
                }
                case "bottom":
                {
                    _loc_6 = param2.x + param4;
                    _loc_7 = param1.y + param1.height + param5;
                    break;
                }
                case LEFT:
                {
                    _loc_6 = param1.x - param2.width + param4;
                    _loc_7 = param2.y + param5;
                    break;
                }
                default:
                {
                    break;
                }
            }
            param2.x = _loc_6;
            param2.y = _loc_7;
        }

        public static function verticalLayout(param1:Array, param2:uint, param3:int = 0, param4:int = 0) : void
        {
            matrixLayout(param1, 1, 0, param2, param3, param4);
        }

        public static function addSpark(param1:DisplayObject) : void
        {
            if (_sparkArr == null)
            {
                _sparkArr = [];
            }
            if (timer == null)
            {
                timer = new EnterFrameTimer(1500);
                timer.addEventListener(TimerEvent.TIMER, timerHandler);
            }
            else if (!timer.hasEventListener(TimerEvent.TIMER))
            {
                timer.addEventListener(TimerEvent.TIMER, timerHandler);
            }
            timer.start();
            if (_sparkArr.indexOf(param1) == -1)
            {
                _sparkArr.push(param1);
            }
        }

        public static function addBg(param1:Class, param2:int, param3:int, param4:int, param5:int, param6:DisplayObjectContainer) : DisplayObject
        {
            var _loc_7:* = new param1 as DisplayObject;
            (new param1 as DisplayObject).x = param2;
            _loc_7.y = param3;
            _loc_7.width = param4;
            _loc_7.height = param5;
            addChild(_loc_7, param6);
            return _loc_7;
        }

        public static function removeSpark(param1:DisplayObject) : void
        {
            if (_sparkArr == null)
            {
                return;
            }
            if (_sparkArr.length > 0)
            {
                if (_sparkArr.indexOf(param1) != -1)
                {
                    _sparkArr.splice(_sparkArr.indexOf(param1), 1);
                    ColorUtil.deHighLight(param1);
                }
            }
            else if (timer != null && timer.hasEventListener(TimerEvent.TIMER))
            {
                timer.removeEventListener(TimerEvent.TIMER, timerHandler);
            }
        }

        public static function hitTestRect(param1:XY, param2:Rectangle) : Boolean
        {
            if (param1.x >= param2.x && param1.x <= param2.x + param2.width && param1.y >= param2.y && param1.y <= param2.y + param2.height)
            {
                return true;
            }
            return false;
        }

        public static function hitTestPoint(stageX:Number, stageY:Number, tester:DisplayObject):Boolean
        {
            if (tester && tester.stage != null)
            {
                return tester.hitTestPoint(stageX, stageY, true);
            }
            return false;
        }

        private static function timerHandler(event:TimerEvent) : void
        {
            if (_sparkArr.length > 0)
            {
                if (_isHeilight)
                {
                    _sparkArr.forEach(showHighLight);
                }
                else
                {
                    _sparkArr.forEach(removeHighLight);
                }
                _isHeilight = !_isHeilight;
            }
        }

        public static function swapDisplayObject(param1:DisplayObject, param2:DisplayObject) : void
        {
            if (param1 != null && param2 != null)
            {
                if (param1.parent != null && param1.parent == param2.parent)
                {
                    param1.parent.swapChildren(param1, param2);
                }
            }
        }

        public static function innerAlign(param1:DisplayObject, param2:DisplayObject, param3:String, param4:int = 0, param5:int = 0) : void
        {
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            switch(param3)
            {
                case "top":
                {
                    _loc_6 = param2.x + param4;
                    _loc_7 = param1.y + param5;
                    break;
                }
                case "right":
                {
                    _loc_6 = param1.x + param1.width - param2.width + param4;
                    _loc_7 = param2.y + param5;
                    break;
                }
                case "bottom":
                {
                    _loc_6 = param2.x + param4;
                    _loc_7 = param1.y + param1.height - param2.height + param5;
                    break;
                }
                case LEFT:
                {
                    _loc_6 = param1.x + param4;
                    _loc_7 = param2.y + param5;
                    break;
                }
                default:
                {
                    break;
                }
            }
            param2.x = _loc_6;
            param2.y = _loc_7;
        }

        public static function toTopOf(param1:DisplayObject, param2:DisplayObject, param3:int = 0) : void
        {
            toXof(param1, param2, "top", param3);
        }

        public static function matrixLayout(param1:Array, param2:uint, param3:uint, param4:uint, param5:int = 0, param6:int = 0) : void
        {
            var _loc_8:* = undefined;
            var _loc_7:int = 0;
            while (_loc_7 < param1.length)
            {
                
                _loc_8 = param1[_loc_7];
                _loc_8.x = param5 + param3 * (_loc_7 % param2);
                _loc_8.y = param6 + param4 * Math.floor(_loc_7 / param2);
                _loc_7++;
            }
        }

        private static function toXof(param1:DisplayObject, param2:DisplayObject, param3:String, param4:int = 0) : void
        {
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            switch(param3)
            {
                case "right":
                {
                    _loc_5 = param1.x + param1.width + param4;
                    _loc_6 = param1.y;
                    break;
                }
                case LEFT:
                {
                    _loc_5 = param1.x - param2.width - param4;
                    _loc_6 = param1.y;
                    break;
                }
                case "top":
                {
                    _loc_5 = param1.x;
                    _loc_6 = param1.y - param2.height - param4;
                    break;
                }
                case "bottom":
                {
                    _loc_5 = param1.x;
                    _loc_6 = param1.y + param1.height + param4;
                    break;
                }
                default:
                {
                    break;
                }
            }
            param2.x = _loc_5;
            param2.y = _loc_6;
        }

        public static function removeAllLevelChild(param1:DisplayObjectContainer) : void
        {
            var _loc_2:DisplayObjectContainer = null;
            if (param1 != null)
            {
                while (param1.numChildren > 0)
                {
                    
                    _loc_2 = param1.getChildAt(0) as DisplayObjectContainer;
                    if (_loc_2 != null)
                    {
                        removeAllLevelChild(_loc_2);
                        continue;
                    }
                    param1.removeChildAt(0);
                }
            }
        }

        public static function addChildBefore(param1:DisplayObject, param2:DisplayObject, param3:DisplayObjectContainer) : void
        {
            var _loc_4:int = 0;
            while (_loc_4 < param3.numChildren)
            {
                
                if (param3.getChildAt(_loc_4) == param2)
                {
                    param3.addChildAt(param1, _loc_4);
                    break;
                }
                _loc_4++;
            }
        }

        public static function corner(param1:DisplayObjectContainer, param2:DisplayObject, param3:String, param4:int = 0, param5:int = 0) : void
        {
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            switch(param3)
            {
                case "topleft":
                {
                    _loc_6 = param4;
                    _loc_7 = param5;
                    break;
                }
                case "topright":
                {
                    _loc_6 = param1.width - param2.width + param4;
                    _loc_7 = param5;
                    break;
                }
                case "bottomleft":
                {
                    _loc_6 = param4;
                    _loc_7 = param1.height - param2.height + param5;
                    break;
                }
                case "bottomright":
                {
                    _loc_6 = param1.width - param2.width + param4;
                    _loc_7 = param1.height - param2.height + param5;
                    break;
                }
                default:
                {
                    break;
                }
            }
            param2.x = _loc_6;
            param2.y = _loc_7;
        }

        public static function removeAllSpark() : void
        {
            if (timer != null && timer.hasEventListener(TimerEvent.TIMER))
            {
                timer.removeEventListener(TimerEvent.TIMER, timerHandler);
            }
            _sparkArr.forEach(removeHighLight);
            _sparkArr = [];
        }

        public static function hitTestObject(param1:DisplayObject, param2:DisplayObject) : Boolean
        {
            if (param1 && param2 && param1.stage != null && param2.stage != null)
            {
                return param1.hitTestObject(param2);
            }
            return false;
        }

        public static function horizontalLayout(param1:Array, param2:uint, param3:int = 0, param4:int = 0) : void
        {
            matrixLayout(param1, param1.length, param2, 0, param3, param4);
        }

        private static function removeHighLight(param1:DisplayObject, param2:int, param3:Array) : void
        {
            ColorUtil.deHighLight(param1);
        }

        public static function isInTheTop(param1:DisplayObject) : Boolean
        {
            var _loc_2:DisplayObjectContainer = null;
            if (param1.parent != null)
            {
                _loc_2 = param1.parent;
                return _loc_2.getChildIndex(param1) == 0;
            }
            return false;
        }

        public static function rowup(param1:Array, param2:int, param3:int = 0, param4:int = 0) : void
        {
            var _loc_6:* = undefined;
            var _loc_5:int = 0;
            while (_loc_5 < param1.length)
            {
                
                _loc_6 = param1[_loc_5];
                _loc_6.x = param3;
                _loc_6.y = param4;
                param4 = _loc_6.y + _loc_6.height + param2;
                _loc_5++;
            }
        }

        public static function addChild(param1:DisplayObject, param2:DisplayObjectContainer) : void
        {
            if (param1 != null && param2 != null)
            {
                param2.addChild(param1);
            }
        }

        public static function toBottomOf(param1:DisplayObject, param2:DisplayObject, param3:int = 0) : void
        {
            toXof(param1, param2, "bottom", param3);
        }

        public static function removeChild(param1:DisplayObject, param2:DisplayObjectContainer) : void
        {
            if (param1 && param2)
            {
                if (param1.parent == param2)
                {
                    param2.removeChild(param1);
                }
            }
        }
    }
}